package com.son.hotel.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.son.hotel.domain.AccommodationInfo;
import com.son.hotel.domain.Rooms;
import com.son.hotel.mapper.AccommodationInfoMapper;
import com.son.hotel.service.AccommodationInfoService;
import com.son.hotel.service.RoomsService;
import com.son.hotel.vo.CustomAndRoomInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class AccommodationInfoServiceImpl extends ServiceImpl<AccommodationInfoMapper, AccommodationInfo>
implements AccommodationInfoService {

    @Autowired
    private RoomsService roomsService;


    @Override
    public void addAccommodationInfo(CustomAndRoomInfo customAndRoomInfo) {
        //计算住宿时长
        DateTime startTime = DateUtil.parse(customAndRoomInfo.getStartTime(), "yyyy-MM-dd");
        DateTime endTime = DateUtil.parse(customAndRoomInfo.getEndTime(), "yyyy-MM-dd");
        int duration = (int)DateUtil.between(startTime, endTime, DateUnit.DAY);
        //计算住宿费用
        int price = customAndRoomInfo.getPrice();
        int cost = price * duration;
        //TODO 根据typeId查询room表获取具体房间号并更改status为1
        QueryWrapper<Rooms> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("typeId",customAndRoomInfo.getTypeId());
        //查询status为0的房间
        queryWrapper.eq("status",0);
        //按升序排序
        queryWrapper.orderByDesc("id");
        //只限制查出一条数据
        //queryWrapper.last("limit 1");
        //查出一条room信息
        Rooms room = roomsService.getOne(queryWrapper);
        log.info("room:{}",room);

    }
}
